package 华为;

import java.util.PriorityQueue;

/**
 * @author zhangmin
 * @create 2022-09-19 11:05
 */
public class Main_0919_2 {
    static class Median{
        PriorityQueue<Integer> small;//大顶堆
        PriorityQueue<Integer> large;//小顶堆
        public Median(){
            large=new PriorityQueue<>();
            small=new PriorityQueue<>((a,b)->{
                return b-a;
            });
        }
        public void addNum(int num){
            if (small.size()<=large.size()){
                large.offer(num);
                small.offer(large.poll());
            }else{
                small.offer(num);
                large.offer(small.poll());
            }
        }
        public double findMedian(){
            if (large.size()<small.size()){
                return small.peek();
            }else if (large.size()>small.size()){
                return large.peek();
            }else {
                return (large.peek()+small.peek())/2.0;
            }
        }
    }
    public static void main(String[] args) {
        Median obj=new Median();
        obj.addNum(1);
        obj.addNum(2);
        System.out.println(obj.findMedian());
        obj.addNum(3);
        System.out.println(obj.findMedian());
    }
}
